package com;

import java.util.Properties;

import org.apache.commons.dbcp2.BasicDataSource;

public class BasicDataSourceUtil {
	private BasicDataSource bds;
	private static BasicDataSourceUtil bdsu;

	/**
	 * 通过构造方法 初始化 链接池对象
	 */
	private BasicDataSourceUtil() {
		// 创建链接池对象
		bds = new BasicDataSource();
		// 解析 properties文件
		Properties properties = PropertiesUtil.getProperties();
		// 设置 数据库信息
		bds.setDriverClassName(properties.getProperty("driver"));
		bds.setUrl(properties.getProperty("url"));
		bds.setUsername(properties.getProperty("username"));
		bds.setPassword(properties.getProperty("password"));
	}

	/**
	 * 对当前类进行单例,保证构造方法执行一次,这样 数据库链接池对象 就只会初始化一次
	 * 
	 * 该单例 应该是 多线程安全, 因为链接池 常用于 多线程环境下
	 * 
	 * @return
	 */
	public static BasicDataSourceUtil getInstance() {
		if (bdsu == null) {
			synchronized (BasicDataSourceUtil.class) {
				if (bdsu == null) {
					bdsu = new BasicDataSourceUtil();
				}
			}
		}
		return bdsu;
	}

	public BasicDataSource getBasicDataSource() {
		return bds;
	}
}
